package tw.asap.plus.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;
import tw.asap.plus.entity.CartBean;
import tw.asap.plus.presenter.CartPresenter;
import tw.asap.plus.ui.activity.OrderConfirmActivity;
import tw.asap.plus.ui.adapter.CategoryAdapter;
import tw.asap.plus.ui.adapter.PinedSectionAdapter;
import tw.asap.plus.ui.custom.PinnedSectionListView;

/**
 * Use the {@link GoodsListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GoodsListFragment extends Fragment {

    private static final String ARG_SECTION_LIST = "arg_section";
    private static final String ARG_ITEM_LIST = "arg_item";

    private ArrayList<PinedSectionAdapter.GoodsItem> goodsItemList;
    private ArrayList<PinedSectionAdapter.GoodsItem> sectionList;

    @InjectView(R.id.mListView)
    PinnedSectionListView mListView;

    @InjectView(R.id.categoryListView)
    ListView mCatListView;

    int currentSection = 0;

    CategoryAdapter categoryAdapter;
    PinedSectionAdapter adapter;
    CartPresenter cartPresenter;


    public static GoodsListFragment newInstance(ArrayList<PinedSectionAdapter.GoodsItem> sectionList, ArrayList<PinedSectionAdapter.GoodsItem> itemList) {
        GoodsListFragment fragment = new GoodsListFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(ARG_SECTION_LIST, sectionList);
        args.putParcelableArrayList(ARG_ITEM_LIST, itemList);
        fragment.setArguments(args);
        return fragment;
    }

    public GoodsListFragment() {
        // Required empty public constructor
    }

    public void setData(List<PinedSectionAdapter.GoodsItem> goodsItemList) {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            sectionList = getArguments().getParcelableArrayList(ARG_SECTION_LIST);
            goodsItemList = getArguments().getParcelableArrayList(ARG_ITEM_LIST);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods_list, container, false);
        ButterKnife.inject(this, view);
        return view;
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        adapter = new PinedSectionAdapter(getActivity(), goodsItemList);
        mListView.setAdapter(adapter);
        mListView.setShadowVisible(false);
        mListView.setOnItemClickListener(adapter);
        categoryAdapter = new CategoryAdapter(getActivity(), sectionList);
        adapter.bindCategory(sectionList, categoryAdapter);
        adapter.bindCartView(cartPresenter);
        sectionList.get(currentSection).isSelect = true;
        mCatListView.setAdapter(categoryAdapter);
        mCatListView.setOnItemClickListener((parent, view1, position, id) -> {
            sectionList.get(currentSection).isSelect = false;
            sectionList.get(position).isSelect = true;
            currentSection = position;
            mListView.setSelection(sectionList.get(position).listPosition);
            categoryAdapter.notifyDataSetChanged();
        });

        mListView.setSectionPinnedListener(sectionPosition -> {

            if (currentSection == sectionPosition) {
                return;
            }
            sectionList.get(currentSection).isSelect = false;
            currentSection = sectionPosition;
            sectionList.get(currentSection).isSelect = true;
            categoryAdapter.notifyDataSetChanged();
            if (sectionPosition >= mCatListView.getLastVisiblePosition()
                    || sectionPosition <= mCatListView.getFirstVisiblePosition()) {
                mCatListView.smoothScrollToPosition(sectionPosition);
            }
        });

    }

    public void bindCartView(CartPresenter cartPresenter) {
        this.cartPresenter = cartPresenter;
        cartPresenter.setOnCheckListener(() -> {
            if(cartPresenter.getCartGoodsList()!=null){
                ArrayList<CartBean> list = (ArrayList<CartBean>) cartPresenter.getCartGoodsList();
                Intent intent = new Intent(getActivity(), OrderConfirmActivity.class);
                intent.putExtra("GOODS_LIST",list);
                intent.putExtras(getActivity().getIntent());
                startActivity(intent);
            }
        });
    }


}
